package com.hmdp.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * @className: com.hmdp.redis-> LeaderboardService
 * @description: 这个类是做做什么得吖，小朋友？
 * @author: chenhs
 * @createDate: 2024-06-25 23:16
 * @version: 1.0
 */
@Service
public class LeaderboardService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private static final String LEADERBOARD_KEY = "scoreRank";

    public void addUserScore(String userId, long score) {
        redisTemplate.opsForZSet().incrementScore(LEADERBOARD_KEY, userId, score);
    }
    public void coverUserScore(String userId, long score) {
        redisTemplate.opsForZSet().add(LEADERBOARD_KEY, userId, score);
    }
    public Set<Object> getTopUsers(int limit) {
        return redisTemplate.opsForZSet().reverseRange(LEADERBOARD_KEY, 0, limit - 1);
    }

    public Long getUserRank(String userId) {
        return redisTemplate.opsForZSet().reverseRank(LEADERBOARD_KEY, userId);
    }

    public Double getUserScore(String userId) {
        return redisTemplate.opsForZSet().score(LEADERBOARD_KEY, userId);
    }
}
